//
//  WWCollectCollectionViewCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/6.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWCollectCollectionViewCell.h"
#import "WWCollectTableCell.h"
#import "WWTableContentModel.h"
#import "AFTools.h"
#import "PublicHeader.h"
#import "Album.h"
#import "CoreDataManager.h"
#import <UIImageView+WebCache.h>

@interface WWCollectCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong)NSArray *hisrotyArray;
@property (nonatomic, strong) UILabel *headerLabel;
@property(nonatomic, strong)CoreDataManager *manager;

@end

@implementation WWCollectCollectionViewCell

- (void)handleData {
    [AFTools getDataByURLString:@"http://mobile.ximalaya.com/mobile/album/recommend/list/unlogin?device=iPhone" WithDataBlock:^(id data) {
        NSArray *tempArr = [NSArray arrayWithArray:[data objectForKey:@"frDatas"]];
        for (NSDictionary *dic in tempArr) {
            WWTableContentModel *tableModel = [WWTableContentModel wwModelWithDictionary:dic];
            [self.dataArray addObject:tableModel];
        }
        [self.tableView reloadData];
    }];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _manager = [CoreDataManager shareManager];
        [self selectAction];
        self.dataArray = [NSMutableArray array];
        [self handleData];
        _tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WWCollectTableCell class] forCellReuseIdentifier:@"tableCell"];
        [_tableView registerClass:[WWCollectTableCell class] forCellReuseIdentifier:@"Cell"];
        self.tableView.rowHeight = 80;
        [self.contentView addSubview:_tableView];
        
        self.tableView.tableHeaderView.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
        [self.headerLabel NightWithType:UIViewColorTypeNormal];
        [self.tableView.tableHeaderView NightWithType:UIViewColorTypeNormal];
        [self.tableView NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

-(void)selectAction
{
    //创建查询请求对象
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Album"];
    //查询结果排序方式
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    //将请求对象发给manager
    _hisrotyArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    [self.tableView reloadData];
//    if (_hisrotyArray.count != array.count) {
//        _hisrotyArray = array;
//        [self.tableView reloadData];
//    }
}

#pragma mark - tableView代理方法

//-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
//{
//    [self selectAction];
//}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_hisrotyArray.count == 0) {
        return 1;
    }
    else{
        return 2;
    }
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_hisrotyArray.count == 0) {
        return self.dataArray.count;
    }
    else{
        if (section == 0) {
            return _hisrotyArray.count;
        }
        else{
            return self.dataArray.count;
        }
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_hisrotyArray.count == 0) {
        WWCollectTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];
        WWTableContentModel *model = self.dataArray[indexPath.row];
        cell.model = model;
        cell.collectButton.hidden = YES;
        return cell;
    }
    else{
        if (indexPath.section == 0) {
            WWCollectTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            Album *albel = _hisrotyArray[indexPath.row];
            cell.titleLabel.text = albel.indexTitle;
            if (albel.indexTitle == nil) {
                cell.titleLabel.text = albel.title;
            }
            cell.introLabel.text = @"";
            cell.countLabel.text = @"";
            [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:albel.imagUrl]];
            cell.isCollection = YES;
            cell.collectButton.hidden = YES;
            [cell buttonClicked];
            return cell;
        }
        else{
            WWCollectTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];
            WWTableContentModel *model = self.dataArray[indexPath.row];
            cell.model = model;
            cell.collectButton.hidden = YES;
            return cell;
        }
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FitConstant_YorHeight(40);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (_hisrotyArray.count == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:FitRectMake(0, 0, 375, 40)];
        [label NightWithType:UIViewColorType3];
        label.text = @"推荐收藏";
        return label;
    }
    else{
        if (section == 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:FitRectMake(0, 0, 375, 40)];
            [label NightWithType:UIViewColorType3];
            label.text = @"收藏列表";
            return label;
        }
        else{
            UILabel *label = [[UILabel alloc] initWithFrame:FitRectMake(0, 0, 375, 40)];
            [label NightWithType:UIViewColorType3];
            label.text = @"推荐收藏";
            return label;
        }
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_hisrotyArray.count == 0) {
        [self.delegate didSelect:_dataArray[indexPath.row] Secion:0 Kind:0];
    }
    else{
        if (indexPath.section == 0) {
            [self.delegate didSelect:_hisrotyArray[indexPath.row] Secion:0 Kind:1];
        }
        else{
            [self.delegate didSelect:_dataArray[indexPath.row] Secion:1 Kind:1];
        }
    }
}


@end
